package org.jsola.hr.web;

import org.jsola.common.StrKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.TransferAccountInfoAddDTO;
import org.jsola.hr.dto.TransferAccountInfoUpdateDTO;
import org.jsola.hr.query.TransferAccountInfoQuery;
import org.jsola.hr.service.ITransferAccountInfoService;
import org.jsola.hr.vo.TransferAccountInfoListVO;
import org.jsola.hr.vo.TransferAccountInfoVO;
import org.jsola.hr.vo.payWx.VxBuildUserToken;
import org.jsola.hr.vo.payWx.VxWgkUserAuthResultVO;
import org.jsola.hr.vo.payWx.checkFace.VxCheckFaceReturnVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 转账账户库
 *
 * @author zhr
 */
@RestController("hrTransferAccountInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/transferAccountInfos")
public class TransferAccountInfoController implements ITransferAccountInfoControllerAPI {

    @Autowired
    private ITransferAccountInfoService transferAccountInfoService;

    @Override
    @PostMapping
    public Result<TransferAccountInfoVO> save(@Valid @RequestBody TransferAccountInfoAddDTO transferAccountInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增转账账户库权限校验
        if (StrKit.isNotEmpty(transferAccountInfoAddDTO.getSiteId())) {
            tokenUser.setSiteId(transferAccountInfoAddDTO.getSiteId());
        }

        return Result.success(
                transferAccountInfoService.save(transferAccountInfoAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody TransferAccountInfoUpdateDTO transferAccountInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改转账账户库权限校验
        if (StrKit.isNotEmpty(transferAccountInfoUpdateDTO.getSiteId())) {
            tokenUser.setSiteId(transferAccountInfoUpdateDTO.getSiteId());
        }

        int count = transferAccountInfoService.update(transferAccountInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{transferAccountInfoId}")
    public Result<Integer> delete(@PathVariable String transferAccountInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除转账账户库权限校验

        int count = transferAccountInfoService.deleteByIds(tokenUser, transferAccountInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/{transferAccountInfoId}/{siteId}")
    public Result<Integer> deleteBySiteId(@PathVariable String transferAccountInfoId, @PathVariable String siteId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除转账账户库权限校验

        tokenUser.setSiteId(siteId);
        int count = transferAccountInfoService.deleteByIds(tokenUser, transferAccountInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] transferAccountInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除转账账户库权限校验

        int count = transferAccountInfoService.deleteByIds(tokenUser, transferAccountInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{transferAccountInfoId}")
    public Result<TransferAccountInfoVO> findById(@PathVariable String transferAccountInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看转账账户库权限校验

        return Result.success(
            transferAccountInfoService.selectById(transferAccountInfoId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<TransferAccountInfoListVO>> listAll(@Valid TransferAccountInfoQuery transferAccountInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看转账账户库权限校验

        return Result.success(
            transferAccountInfoService.select(transferAccountInfoQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<TransferAccountInfoListVO>> page(@Valid TransferAccountInfoQuery transferAccountInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            transferAccountInfoService.selectPage(transferAccountInfoQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/pageNoSiteId")
    public Result<Page<TransferAccountInfoListVO>> pageNoSiteId(@Valid TransferAccountInfoQuery transferAccountInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        Page<TransferAccountInfoListVO> page = transferAccountInfoService.selectPage(transferAccountInfoQuery, null);
        return Result.success(
                page
        );
    }

    @Override
    @GetMapping("/selectVxAccountMoney")
    public Result<List<TransferAccountInfoVO>> selectVxAccountMoney() {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                transferAccountInfoService.selectVxAccountMoney(tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/generateZfbSignUrl/{loginId}")
    public Result<String> generateZfbSignUrl(@PathVariable String loginId) {

        return Result.success(
                transferAccountInfoService.generateZfbSignUrl(loginId)
        );
    }

    @Override
    @PutMapping("/generateZfbSafetyPayAccountBook/{transferAccountInfoId}")
    public Result<String> generateZfbSafetyPayAccountBook(@PathVariable String transferAccountInfoId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                transferAccountInfoService.generateZfbSafetyPayAccountBook(transferAccountInfoId, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectZfbSafetyAccountMoney")
    public Result<List<TransferAccountInfoVO>> selectZfbSafetyAccountMoney() {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                transferAccountInfoService.selectZfbSafetyAccountMoney(tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/generateZfbMoneyToBookUrl/{transferAccountInfoId}/{money}")
    public Result<String> generateZfbMoneyToBookUrl(@PathVariable String transferAccountInfoId, @PathVariable Double money) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                transferAccountInfoService.generateZfbMoneyToBookUrl(transferAccountInfoId, money, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectUserAuthStatus/{openId}/{siteId}")
    public Result<VxWgkUserAuthResultVO> selectUserAuthStatus(@PathVariable String openId, @PathVariable String siteId) {
        return Result.success(
                transferAccountInfoService.selectUserAuthStatus(openId, siteId)
        );
    }

    @Override
    @PostMapping("/buildAuthToken/{siteId}")
    public Result<VxCheckFaceReturnVO> buildAuthToken(@RequestBody VxBuildUserToken vxBuildUserToken, @PathVariable String siteId) {
        return Result.success(
                transferAccountInfoService.buildAuthToken(vxBuildUserToken, siteId)
        );
    }

    @Override
    @GetMapping("/selectPingAnBankAccountMoney")
    public Result<List<TransferAccountInfoVO>> selectPingAnBankAccountMoney() {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                transferAccountInfoService.selectPingAnBankAccountMoney(tokenUser.getSiteId())
        );
    }


}

